//
//  videosViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-4.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "videosViewController.h"
#import "videosView.h"
#import "videoTableViewCell.h"
#import "movieHandel.h"
#import "movieDataModel.h"
#import "UIImage+ImageDown.h"
#import "videoDetailViewController.h"
#import "UITableView+DragLoad.h"
@interface videosViewController ()<UITableViewDataSource,UIScrollViewDelegate,UITableViewDelegate,UITableViewDragLoadDelegate>
{
   BOOL _dragRefresh;

}
@property(nonatomic,strong)videosView *video;
@end

@implementation videosViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"视频";
        
        self.video = [[[videosView alloc] initWithFrame: [UIScreen mainScreen].bounds] autorelease];
        
    }
    return self;
}
-(void)loadView{

    self.view = self.video;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [movieHandel movieshareData];
    
    _dragRefresh = YES;
    [movieHandel movieshareData].sdr = 0;
    
    
    [[movieHandel movieshareData] requestMovieDataWithString:kmoviewdataString];
   
    
  
    
    //设置代理
    for (int i = 0; i < 4; i++) {
        UITableView* tab = (UITableView *)[self.video.scrll viewWithTag:100 + i];
        
        tab.delegate = self;
        
        //设置数据源代理
        tab.dataSource = self;
        
        //设置刷新代理
       [tab setDragDelegate:self refreshDatePermanentKey:[NSString stringWithFormat:@"%d",i]];
        
    }
    
    //设置scrlloview的代理
    self.video.scrll.delegate = self;
    //分别给4个button添加事件
    [self.video.button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.video.button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.video.button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.video.button4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加kvo
    [[movieHandel movieshareData] addObserver:self forKeyPath:@"hostArray" options:    NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld  context:nil];
 
 
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
    while ([keyPath isEqualToString:@"hostArray"]) {
        UITableView *tab = (UITableView *)[self.video.scrll viewWithTag:100];
        if (_dragRefresh) {
            [tab finishRefresh];
            [tab reloadData];
            break;
        }else{
            [tab finishLoadMore];
            [tab reloadData];
            break;
        
        }
    }
    
    

    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//每组cell的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if(tableView.tag == 100){
        
        //NSLog(@"%d",[[movieHandel movieshareData].hostArray[section] count]);

       // NSLog(@"%d",[[movieHandel movieshareData].hostArray[section] count]);

    return [[movieHandel movieshareData].hostArray[section] count];
       
      
    }else if(tableView.tag == 101){
      return 20;
    }else if(tableView.tag == 102){
       return 20;
    }else {
    return 20;
    }


 

}

//几组cell
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    // NSLog(@"qqqqq:%d",[movieHandel movieshareData].hostArray.count);
    if (tableView.tag == 100) {
    return [movieHandel movieshareData].hostArray.count;
    }else if(tableView.tag == 101){
        return 1;
    }else if(tableView.tag == 102){
        return 1;
    }else {
        return 1;
    }

   
}

//创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString *cell_id = @"cell_id";
    
    videoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    
    if (nil == cell) {
        cell = [[[videoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id] autorelease];
        }
    
    

   if (tableView.tag == 100) {
        
        movieDataModel *mda = [movieHandel movieshareData].hostArray[indexPath.section][indexPath.row];
   
     cell.movieView.image = [UIImage imageWithUrlString:mda.cover myBlock:^(UIImage *image) {
            cell.movieView.image = image;
//
      }];
       cell.titlabel.text = mda.title;
       
    }else if(tableView.tag == 101){
        
    }else if(tableView.tag == 102){
        
    }else if(tableView.tag == 103){
        
    }

    
    return cell;
   
}
//hearder的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 180;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    //给详情接口传入vid
    movieDataModel *m = [movieHandel movieshareData].hostArray[indexPath.section][indexPath.row];
    [movieHandel movieshareData].str = m.vid;
    
    //NSLog(@"%@",m.vid);
    //模态跳入详情页面
    videoDetailViewController *videotail = [[[videoDetailViewController alloc] init] autorelease];
    videotail.str_url = m.mp4_url;
    //NSLog(@"%@",videotail.str_url);
    videotail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    videotail.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:videotail animated:YES completion:nil];
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int i =  self.video.scrll.contentOffset.x/[UIScreen mainScreen].bounds.size.width + 200;
   
    //判断是否为当前的button改变button字体的颜色
    if (self.video.button1.tag == i) {
        [self.video.button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        
        [self.video.button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    //
    if (self.video.button2.tag == i) {
        [self.video.button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        
        [self.video.button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
    //
    if (self.video.button3.tag == i) {
        [self.video.button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        
        [self.video.button3 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
    //
    if (self.video.button4.tag == i) {
        [self.video.button4 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        
        [self.video.button4 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
    
    
}

//button事件
-(void)buttonAction:(UIButton *)sender {
    
    
    //int i = sender.tag - 200;
    [UIView animateWithDuration:0.3 animations:^{
        
     
        self.video.scrll.contentOffset = CGPointMake(320 *(sender.tag - 200), 0);
        
    }];
    
    
    if (sender.tag == 200) {
        [self.video.button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        
        [self.video.button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
  
   
    if (sender.tag == 201) {
        [self.video.button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        [self.video.button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
    if (sender.tag == 202) {
        [self.video.button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        [self.video.button3 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
    if (sender.tag == 203) {
        [self.video.button4 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }else{
        [self.video.button4 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
    
}


//刷新的代理方法

#pragma mark 请求数据
- (void)dragTableDidTriggerRefresh:(UITableView *)tableView{

    _dragRefresh = YES;
    //请求数据
   // [[movieHandel movieshareData].hostdataArray removeAllObjects];
  
    if (tableView.tag == 100) {
        
        [[movieHandel movieshareData] requestMovieDataWithString:kmoviewdataString];
      }
}
//加载的代理方法
- (void)dragTableDidTriggerLoadMore:(UITableView *)tableView{

    _dragRefresh = NO;
    [movieHandel movieshareData].sdr += 10;
    
    if (tableView.tag == 100) {
        
        [[movieHandel movieshareData] requestMovieDataWithString:kmoviewdataString];
    }

}

@end
